package com.kzw.oa.system.web;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kzw.misc.utils.ContextUtil;
import com.kzw.oa.system.bean.TopModule;
import com.kzw.oa.system.model.AppUser;

/**
 * 首页
 */
@Controller
public class IndexAction {

	@RequestMapping("/index")
	public String index(@CookieValue(defaultValue = "") String topNavId, Model model, HttpServletResponse response) {

		AppUser currUser = ContextUtil.getCurrentUser();
		model.addAttribute("currUser", currUser);

		// 学生登陆，跳转到学生首页
		if(currUser.getRoleNames().contains("学生")) {
			return "redirect:/exam/student/index";
		}
		
		// 显示的模块名
		Map<String, TopModule> topMaps = currUser.getTopModules();
		if (StringUtils.isBlank(topNavId) || !topMaps.containsKey(topNavId)) {
			topNavId = topMaps.keySet().iterator().next();
		}
		model.addAttribute("topNavId", topNavId.replace("Mod_", ""));

		Cookie cookie = new Cookie("topNavId", topNavId);
		cookie.setPath("/");
		cookie.setMaxAge(30 * 24 * 3600);
		response.addCookie(cookie);

		return "index";
	}
}
